<?php

/**
 * Menashop
 *
 * LICENSE
 *
 * This source file is subject to the license that is bundled
 * with this project in the file LICENSE.txt
 *
 * @category   Zend
 * @package    Admin_Model
 * @subpackage Customer
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 * @version    1.0.0
 * @since      File available since Release 1.0.0
 */

/**
 * The domain for customer
 * 
 * @category   Zend
 * @package    Admin_Model
 * @subpackage Customer
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 */
class Admin_Model_Customer extends Zend_Db_Table_Abstract
{

    /**
     * The name of table
     * @var string 
     */
    protected $_name = 'customer';

    /**
     * Save customer
     * @param array $row
     * @return integer
     */
    public function save($row)
    {
        // check ID (insert or update)
        $id = isset($row['id']) ? $row['id'] : null;
        if ($id) {
            unset($row['id']);
            $this->update($row, array('id = ?' => $id));
        } else {
            $this->insert($row);
        }
    }

    /**
     * Get customers by filter 
     * @return array 
     */
    public function getAllByFilter($id, $fullName, $phone, $email, $city, $address, $totalFrom, $totalTo, $createdFrom, $createdTo, $sort, $direction, $page, $rowCount)
    {
        $query = $this->getAdapter()->select();
        $query->from(array($this->_name), array('id', 'full_name', 'phone', 'email', 'city', 'address', 'total', 'created'));

        // Check filters
        if ($id) {
            $query->where('id = ?', $id);
        }
        if ($fullName) {
            $query->where('full_name LIKE ?', $fullName . '%');
        }
        if ($phone) {
            $query->where('phone LIKE ?', $phone . '%');
        }
        if ($email) {
            $query->where('email LIKE ?', $email . '%');
        }
        if ($city) {
            $query->where('city LIKE ?', $city . '%');
        }
        if ($address) {
            $query->where('address LIKE ?', $address . '%');
        }
        if ($totalFrom) {
            $query->where('total >= ?', $totalFrom);
        }
        if ($totalTo) {
            $query->where('total <= ?', $totalTo);
        }
        if ($createdFrom) {
            $query->where('created >= ?', $createdFrom);
        }
        if ($createdTo) {
            $query->where('created <= ?', $createdTo);
        }

        if ($sort && $direction) {
            $query->order($sort . ' ' . $direction);
        }

        $query->limitPage($page, $rowCount);

        // Get customers from DB
        $result = $this->getAdapter()->fetchAll($query, null, Zend_Db::FETCH_OBJ);
        return $result;
    }

    /**
     * Get count of customers by filter 
     * @return integer 
     */
    public function getCountByFilter($id, $fullName, $phone, $email, $city, $address, $totalFrom, $totalTo, $createdFrom, $createdTo)
    {
        $query = $this->getAdapter()->select();
        $query->from($this->_name, new Zend_Db_Expr('COUNT(*)'));

        // Check filters
        if ($id) {
            $query->where('id = ?', $id);
        }
        if ($fullName) {
            $query->where('full_name LIKE ?', $fullName . '%');
        }
        if ($phone) {
            $query->where('phone LIKE ?', $phone . '%');
        }
        if ($email) {
            $query->where('email LIKE ?', $email . '%');
        }
        if ($city) {
            $query->where('city LIKE ?', $city . '%');
        }
        if ($address) {
            $query->where('address LIKE ?', $address);
        }
        if ($totalFrom) {
            $query->where('total >= ?', $totalFrom);
        }
        if ($totalTo) {
            $query->where('total <= ?', $totalTo);
        }
        if ($createdFrom) {
            $query->where('created >= ?', $createdFrom);
        }
        if ($createdTo) {
            $query->where('created <= ?', $createdTo);
        }

        // Get count of customers from DB
        $result = $this->getAdapter()->fetchOne($query);

        return $result;
    }

    /**
     * Get customer by ID
     * @param integer $customerId
     * @return array 
     */
    public function getById($customerId)
    {
        $query = $this->getAdapter()->select();
        $query->from($this->_name, array('id', 'full_name', 'phone', 'email', 'city', 'address', 'total', 'created'));
        $query->where('id = ?', $customerId);

        // Get customer from DB
        $result = $this->getAdapter()->fetchRow($query, null, Zend_Db::FETCH_OBJ);
        return $result;
    }

}
